package com.myebills.ebpsfrontend.cust.maxis;

import com.myebills.ebpsfrontend.cust.controller.*;
import com.myebills.ebpsfrontend.cust.utils.*;
import com.checkfree.isolutions.customer.persist.Account;
import com.checkfree.isolutions.customer.persist.User;
import com.checkfree.isolutions.admin.ObjectId;
import com.checkfree.isolutions.statement.persist.Statement;
import com.checkfree.isolutions.utility.CFiError;
import com.checkfree.isolutions.property.PropertiesConfig;

import java.awt.Color;
import java.io.FileOutputStream;
import java.io.*;
import java.util.*;
import java.text.*;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.servlet.*;
import javax.servlet.http.*;

import com.lowagie.text.*;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfWriter;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Chunk;
import com.lowagie.text.Font;
import com.lowagie.text.Phrase;
import com.lowagie.text.Element;
import com.lowagie.text.Image;

import com.checkfree.isolutions.customer.persist.*;
import com.checkfree.isolutions.statement.*;
import com.checkfree.isolutions.statement.persist.*;
import com.checkfree.isolutions.property.Properties;
import com.checkfree.isolutions.property.PropertiesConfig;



public class PDFServlet_v1r00 extends EbppxTransactionServlet {

    public void doTransaction(HttpServletRequest req, HttpServletResponse response)
    throws ServletException {
        try {
            final HttpSession session = req.getSession(false);
            if (null == session) {
                throw new ServletException("Session must be already established");
            }

            final EbppxUrlManager urlManager = getUrlManager(req);

            final String stmtIndex = urlManager.getSecureParameter("stmtIndex");
            if (stmtIndex.length() == 0) {
                throw new ServletException("Missing statement index");
            }

            final int stmtIdx = Integer.parseInt(stmtIndex);

            final String sOidAccount = urlManager.getSecureParameter(EbppxConstants.ACCOUNT_ID);
            if (sOidAccount.length() == 0) {
                throw new ServletException("Missing account identifier");
            }

			//get information from checkfree database
            final Account cfiAccount = getAccountHome().findById(new ObjectId(sOidAccount));
            final Vector cfiStatements = StatementUtil.getStatements(cfiAccount,EbppxCusConstants.STMT_START_MONTH);
            final Statement cfiStatement = StatementUtil.getStatement(cfiAccount,EbppxCusConstants.STMT_START_MONTH,stmtIdx);

            String stmtIndexPrev = "-1";
            if ((stmtIdx+1) < cfiStatements.size()) {
                stmtIndexPrev = String.valueOf(stmtIdx+1);
            }

           	final StatementSection cfiRootSection = cfiStatement.getRootSection();
		   	final StatementSection cfiSummarySection = cfiRootSection.getSection("SummarySection");
		   	final StatementSection cfiRegistrationChargesSection = cfiSummarySection.getSection("RegistrationChargesSection");
		   	final StatementSection cfiMonthlyChargesSection = cfiSummarySection.getSection("MonthlyChargesSection");
		   	final StatementSection cfiValueAddedServicesSection = cfiSummarySection.getSection("ValueAddedServicesSection");
		   	final StatementSection cfiUsageChargesSection = cfiSummarySection.getSection("UsageChargesSection");
		   	final StatementSection cfiAdvantageSavingsSection = cfiSummarySection.getSection("AdvantageSavingsSection");
		   	final StatementSection cfiOtherChargesSection = cfiSummarySection.getSection("OtherChargesSection");
		   	final StatementSection cfiDiscountSection = cfiSummarySection.getSection("DiscountSection");
			final StatementSection cfiCurrentChargesBeforeTaxSection = cfiSummarySection.getSection("CurrentChargesBeforeTaxSection");
			final StatementSection cfiServiceTaxSection = cfiSummarySection.getSection("ServiceTaxSection");
			final StatementSection cfiImportantMessagesSection = cfiRootSection.getSection("ImportantMessagesSection");

		  	final String AccName = EbppxUtil.htmlEncode(cfiRootSection.getFieldText("AccountName"));
		  	final String AccNo = EbppxUtil.htmlEncode(cfiRootSection.getFieldText("AccountNumber"));
		  	final String PhoneNo = EbppxUtil.htmlEncode(cfiRootSection.getFieldText("PhoneNumber"));
		  	final String RefNo = EbppxUtil.htmlEncode(cfiRootSection.getFieldText("ReferenceNumber"));
		  	final String StmtDt = EbppxUtil.htmlEncode(cfiRootSection.getFieldText("StatementDate"));
		  	final String BillPeriod = EbppxUtil.htmlEncode(cfiRootSection.getFieldText("BillingPeriod"));
		  	final String DepositPaid = EbppxUtil.htmlEncode(cfiRootSection.getFieldText("DepositPaid"));
		  	final String TotalAmountDue = EbppxUtil.htmlEncode(cfiRootSection.getFieldText("TotalAmountDue"));
		  	final String CurrentChargesDueDate = EbppxUtil.htmlEncode(cfiRootSection.getFieldText("CurrentChargesDueDate"));
		  	final String BalanceForward = EbppxUtil.htmlEncode(cfiSummarySection.getFieldText("BalanceForward"));
		  	final String TotalCurrentCharges = EbppxUtil.htmlEncode(cfiSummarySection.getFieldText("TotalCurrentCharges"));
		  	final String PreviousBalance = EbppxUtil.htmlEncode(cfiSummarySection.getFieldText("PreviousBalance"));
		  	final String PaymentReceivedDate = EbppxUtil.htmlEncode(cfiSummarySection.getFieldText("PaymentReceivedDate"));
		  	final String PaymentReceived = EbppxUtil.htmlEncode(cfiSummarySection.getFieldText("PaymentReceived"));
		  	final String Adjustment = EbppxUtil.htmlEncode(cfiSummarySection.getFieldText("Adjustment"));
			final String RegistrationCharges = EbppxUtil.htmlEncode(cfiRegistrationChargesSection.getFieldText("Amount"));
			final String MonthlyCharges = EbppxUtil.htmlEncode(cfiMonthlyChargesSection.getFieldText("Amount"));
			final String ValueAddedServices = EbppxUtil.htmlEncode(cfiValueAddedServicesSection.getFieldText("Amount"));
			final String UsageCharges = EbppxUtil.htmlEncode(cfiUsageChargesSection.getFieldText("Amount"));
			final String OtherCharges = EbppxUtil.htmlEncode(cfiOtherChargesSection.getFieldText("Amount"));
			final String Discount = EbppxUtil.htmlEncode(cfiDiscountSection.getFieldText("Amount"));
			final String CurrentChargesBeforeTax = EbppxUtil.htmlEncode(cfiCurrentChargesBeforeTaxSection.getFieldText("Amount"));
			final String ServiceTax = EbppxUtil.htmlEncode(cfiServiceTaxSection.getFieldText("Amount"));

			//2007-02-13 checking if the section or field exist LTH
			String AdvantageSavings = "";
			if (cfiAdvantageSavingsSection != null){
				if (EbppxUtil.htmlEncode(cfiAdvantageSavingsSection.getFieldText("Amount")) != null){
					AdvantageSavings=EbppxUtil.htmlEncode(cfiAdvantageSavingsSection.getFieldText("Amount"));
				}
			}
			String MessageLine1 = "";
			if (EbppxUtil.htmlEncode(cfiImportantMessagesSection.getFieldText("MessageLine5")) != null){
				MessageLine1 = EbppxUtil.htmlEncode(cfiImportantMessagesSection.getFieldText("MessageLine5"));
			}
			String MessageLine2 = "";
			if (EbppxUtil.htmlEncode(cfiImportantMessagesSection.getFieldText("MessageLine6")) != null){
				MessageLine2 = EbppxUtil.htmlEncode(cfiImportantMessagesSection.getFieldText("MessageLine6"));
			}
			String MessageLine3 = "";
			if (EbppxUtil.htmlEncode(cfiImportantMessagesSection.getFieldText("MessageLine8")) != null){
				MessageLine3 = EbppxUtil.htmlEncode(cfiImportantMessagesSection.getFieldText("MessageLine8"));
			}
			String MessageLine4 = "";
			if (EbppxUtil.htmlEncode(cfiImportantMessagesSection.getFieldText("MessageLine9")) != null){
				MessageLine4 = EbppxUtil.htmlEncode(cfiImportantMessagesSection.getFieldText("MessageLine9"));
			}
			String MessageLine5 = "";
			if (EbppxUtil.htmlEncode(cfiImportantMessagesSection.getFieldText("MessageLine11")) != null){
				MessageLine5 = EbppxUtil.htmlEncode(cfiImportantMessagesSection.getFieldText("MessageLine11"));
			}
			String MessageLine6 = "";
			if (EbppxUtil.htmlEncode(cfiImportantMessagesSection.getFieldText("MessageLine13")) != null){
				MessageLine6 = EbppxUtil.htmlEncode(cfiImportantMessagesSection.getFieldText("MessageLine13"));
			}
			String MessageLine7 = "";
			if (EbppxUtil.htmlEncode(cfiImportantMessagesSection.getFieldText("MessageLine14")) != null){
				MessageLine7 = EbppxUtil.htmlEncode(cfiImportantMessagesSection.getFieldText("MessageLine14"));
			}
			String MessageLine8 = "";
			if (EbppxUtil.htmlEncode(cfiImportantMessagesSection.getFieldText("MessageLine15")) != null){
				MessageLine8 = EbppxUtil.htmlEncode(cfiImportantMessagesSection.getFieldText("MessageLine15"));
			}
			String MessageLine9 = "";
			if (EbppxUtil.htmlEncode(cfiImportantMessagesSection.getFieldText("MessageLine17")) != null){
				MessageLine9 = EbppxUtil.htmlEncode(cfiImportantMessagesSection.getFieldText("MessageLine17"));
			}



			final Date dtVal = cfiStatement.getDate();
            final DateFormat dfdisp = new SimpleDateFormat("ddMMMyyyy");
            String stdate = EbppxUtil.htmlEncode(dfdisp.format(dtVal));

            String filename = new String(cfiAccount.getExternalKey()+"-"+stdate);
           	filename += ".pdf";

		   	response.setContentType("application/pdf");
		   	 //response.setHeader("Content-Disposition","attachment; filename=" + filename);

		   					Document document = new Document();

		   						PdfWriter.getInstance(document,response.getOutputStream());
		   						document.open();

								//Write data to PDF

                                //Crate Table for page position purpose
                                float[] tableColsWidth = {1f, 1f};
                                PdfPTable table = new PdfPTable(tableColsWidth);

                                //set the table width percentage of the page
                                table.setWidthPercentage(100);

								//Declare
								Chunk chunk;
								Phrase phrase;
								Paragraph paragraph;
								PdfPCell cell;
								Font font = FontFactory.getFont(FontFactory.HELVETICA, 8, Font.NORMAL,new Color(0, 0, 0));
								Font fontItalic = FontFactory.getFont(FontFactory.HELVETICA, 8, Font.ITALIC,new Color(0, 0, 0));
								Font fontBold = FontFactory.getFont(FontFactory.HELVETICA, 8, Font.BOLD,new Color(0, 0, 0));
								Font bigFontItalic = FontFactory.getFont(FontFactory.HELVETICA, 12, Font.ITALIC,new Color(0, 0, 0));
								Font bigFontBold = FontFactory.getFont(FontFactory.HELVETICA, 12, Font.BOLD,new Color(0, 0, 0));

								//Font font = new Font(Font.TIMES_ROMAN, 8, Font.NORMAL);
								//Font fontItalic = new Font(Font.TIMES_ROMAN, 8, Font.ITALIC);
								//Font fontBold = new Font(Font.TIMES_ROMAN, 8, Font.BOLD);
								//Font bigFontItalic = new Font(Font.TIMES_ROMAN, 12, Font.ITALIC);
								//Font bigFontBold = new Font(Font.TIMES_ROMAN, 12, Font.BOLD);

								String ImagePath = "";

								//Title
								phrase = new Phrase(20, "");
								chunk = new Chunk("STATEMENT OF ACCOUNT\n", bigFontBold);
								phrase.add(chunk);
								chunk = new Chunk("PENYATA AKAUN", bigFontItalic);
								phrase.add(chunk);
								paragraph = new Paragraph();
                                paragraph.add(phrase);
                                cell = new PdfPCell(paragraph);
								cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
								cell.setVerticalAlignment(Element.ALIGN_BOTTOM);
								cell.setColspan(2);
								cell.setBorderWidth(0);
								table.addCell(cell);

								//add blank row at main table
								cell = new PdfPCell(new Paragraph("          "));
								cell.setHorizontalAlignment(Element.ALIGN_CENTER);
								cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
								cell.setColspan(2);
								cell.setBorderWidth(0);
								table.addCell(cell);

								//Account detail
								phrase = new Phrase(15, "");
								chunk = new Chunk("Account Name/", font);
								phrase.add(chunk);
								chunk = new Chunk("Nama Akaun       ", fontItalic);
								phrase.add(chunk);
								chunk = new Chunk(": " + AccName + "\n\n", font);
								phrase.add(chunk);


								chunk = new Chunk("Account No/", font);
								phrase.add(chunk);
								chunk = new Chunk("No.Akaun                 ", fontItalic);
								phrase.add(chunk);
								chunk = new Chunk(": " + AccNo + "\n\n", font);
								phrase.add(chunk);

								chunk = new Chunk("Phone Number/", font);
								phrase.add(chunk);
								chunk = new Chunk("No.Telefon          ", fontItalic);
								phrase.add(chunk);
								chunk = new Chunk(": " + PhoneNo + "\n\n", font);
								phrase.add(chunk);

								chunk = new Chunk("Reference Number/", font);
								phrase.add(chunk);
								chunk = new Chunk("No.Rujukan   ", fontItalic);
								phrase.add(chunk);
								chunk = new Chunk(": " + RefNo + "\n\n", font);
								phrase.add(chunk);

								chunk = new Chunk("Statement Date/", font);
								phrase.add(chunk);
								chunk = new Chunk("Tarikh Penyata   ", fontItalic);
								phrase.add(chunk);
								chunk = new Chunk(": " + StmtDt + "\n\n", font);
								phrase.add(chunk);

								chunk = new Chunk("Billing Period/", font);
								phrase.add(chunk);
								chunk = new Chunk("Tempoh Bil             ", fontItalic);
								phrase.add(chunk);
								chunk = new Chunk(": " + BillPeriod + "\n\n", font);
								phrase.add(chunk);

								chunk = new Chunk("Deposit Paid/", font);
								phrase.add(chunk);
								chunk = new Chunk("Cagaran                  ", fontItalic);
								phrase.add(chunk);
								chunk = new Chunk(": " + DepositPaid + "\n\n", font);
								phrase.add(chunk);

                                paragraph = new Paragraph();
                                paragraph.add(phrase);

								//Get Image Detail from config
								com.checkfree.isolutions.property.Properties appCfg = com.checkfree.isolutions.property.PropertiesConfig.getApplicationSettings();

								if (appCfg.hasProperty("PdfPrePrintedForm") == false){
								throw new IllegalArgumentException("Image Path not specified!");
								}else{
								ImagePath = appCfg.getProperty("PdfPrePrintedForm");

								//Add Maxis Image Background
								Image image = Image.getInstance(ImagePath);
								//image.setAbsolutePosition(-35, 3);
								//image.scalePercent(145,135);
								image.setAbsolutePosition(-40, 0);
								image.scalePercent(85,85);
								//image.scaleToFit(750,850);
								//image.scalePercent( 72f / image.getDpiX() * 100 );
								image.setAlignment(Image.UNDERLYING);
								document.add(image);
}

								cell = new PdfPCell(paragraph);
								cell.setHorizontalAlignment(Element.ALIGN_LEFT);
								cell.setBorderWidth(0);
		                        table.addCell(cell);


								//Create Table2 for Total Amount Due and current charges due date
								//float[] table2ColsWidth = {1f, 1f};
                                PdfPTable table2 = new PdfPTable(2);

                                //table2.setWidthPercentage(100);

								phrase = new Phrase(20, "");
								chunk = new Chunk("\nTotal Amount Due\n", fontBold);
								phrase.add(chunk);
								chunk = new Chunk("Jumlah Perlu Dibayar\n\n", fontItalic);
								phrase.add(chunk);
								paragraph = new Paragraph();
                                paragraph.add(phrase);
                                cell = new PdfPCell(paragraph);
								cell.setHorizontalAlignment(Element.ALIGN_CENTER);
								//cell.setBackgroundColor(Color.GREEN);
								cell.setGrayFill(0.8f);
								//cell.setMinimumHeight(10);
								//cell.setColspan(2);
								table2.addCell(cell);

								phrase = new Phrase(20, "");
								chunk = new Chunk("\nCurrent Charges Due Date\n", fontBold);
								phrase.add(chunk);
								chunk = new Chunk("Tarikh Akhir Bayaran Caj Semasa\n\n", fontItalic);
								phrase.add(chunk);
								paragraph = new Paragraph();
                                paragraph.add(phrase);
								cell = new PdfPCell(paragraph);
								cell.setHorizontalAlignment(Element.ALIGN_CENTER);
								//cell.setBackgroundColor(Color.GREEN);
								cell.setGrayFill(0.8f);
								//cell.setMinimumHeight(10);
		                        table2.addCell(cell);

		                        cell = new PdfPCell(new Paragraph("\n" + TotalAmountDue + "\n\n", fontBold));
		                        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
		                        //cell.setMinimumHeight(10);
		   						table2.addCell(cell);

		   						cell = new PdfPCell(new Paragraph("\n" + CurrentChargesDueDate + "\n\n", fontBold));
		   						cell.setHorizontalAlignment(Element.ALIGN_CENTER);
		   						//cell.setMinimumHeight(10);
		   						table2.addCell(cell);

								cell = new PdfPCell(table2);
								cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
								cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
								cell.setBorderWidth(0);
								table.addCell(cell);

								//Create table 3 to hold data
								PdfPTable table3 = new PdfPTable(2);

                                //table3.setWidthPercentage(100);
                                float[] inTableColsWidth = {2f, 1f};
								PdfPTable inTable = new PdfPTable(inTableColsWidth);

                                phrase = new Phrase(15, "");
								chunk = new Chunk("Previous Charges\n", fontBold);
								phrase.add(chunk);
								chunk = new Chunk("Caj Dahulu", fontItalic);
								phrase.add(chunk);
								paragraph = new Paragraph();
                                paragraph.add(phrase);
                                cell = new PdfPCell(paragraph);
								cell.setHorizontalAlignment(Element.ALIGN_LEFT);
								cell.setBorderWidth(0);
								inTable.addCell(cell);

								phrase = new Phrase(15, "");
								chunk = new Chunk("Amount(RM)\n", fontBold);
								phrase.add(chunk);
								chunk = new Chunk("Amaun(RM)", fontItalic);
								phrase.add(chunk);
								paragraph = new Paragraph();
                                paragraph.add(phrase);
                                cell = new PdfPCell(paragraph);
								cell.setHorizontalAlignment(Element.ALIGN_LEFT);
								cell.setBorderWidth(0);
								inTable.addCell(cell);

								cell = new PdfPCell(inTable);
								cell.setGrayFill(0.8f);
								table3.addCell(cell);

								//Reuse inTable
								inTable = new PdfPTable(inTableColsWidth);

								phrase = new Phrase(15, "");
								chunk = new Chunk("Current Charges\n", fontBold);
   								phrase.add(chunk);
								chunk = new Chunk("Caj Semasa", fontItalic);
								phrase.add(chunk);
								paragraph = new Paragraph();
                                paragraph.add(phrase);
                                cell = new PdfPCell(paragraph);
								cell.setHorizontalAlignment(Element.ALIGN_LEFT);
								cell.setBorderWidth(0);
								inTable.addCell(cell);

								phrase = new Phrase(15, "");
								chunk = new Chunk("Amount(RM)\n", fontBold);
								phrase.add(chunk);
								chunk = new Chunk("Amaun(RM)", fontItalic);
								phrase.add(chunk);
								paragraph = new Paragraph();
                                paragraph.add(phrase);
                                cell = new PdfPCell(paragraph);
								cell.setHorizontalAlignment(Element.ALIGN_LEFT);
								cell.setBorderWidth(0);
								inTable.addCell(cell);

								cell = new PdfPCell(inTable);
								cell.setGrayFill(0.8f);
								table3.addCell(cell);

								//Reuse inTable for Previous Charges in table3
								inTable = new PdfPTable(inTableColsWidth);

								phrase = new Phrase(15, "");
								chunk = new Chunk("Previous Balance", font);
   								phrase.add(chunk);
								paragraph = new Paragraph();
                                paragraph.add(phrase);
                                cell = new PdfPCell(paragraph);
								cell.setHorizontalAlignment(Element.ALIGN_LEFT);
								cell.setBorderWidth(0);
								inTable.addCell(cell);

								phrase = new Phrase(15, "");
								chunk = new Chunk(PreviousBalance, font);
   								phrase.add(chunk);
								paragraph = new Paragraph();
                                paragraph.add(phrase);
                                cell = new PdfPCell(paragraph);
								cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
								cell.setPaddingRight(40f);
								cell.setBorderWidth(0);
								inTable.addCell(cell);

								phrase = new Phrase(15, "");
								chunk = new Chunk(PaymentReceivedDate, font);
   								phrase.add(chunk);
								paragraph = new Paragraph();
                                paragraph.add(phrase);
                                cell = new PdfPCell(paragraph);
								cell.setHorizontalAlignment(Element.ALIGN_LEFT);
								cell.setBorderWidth(0);
								inTable.addCell(cell);

								phrase = new Phrase(15, "");
								chunk = new Chunk(PaymentReceived, font);
   								phrase.add(chunk);
								paragraph = new Paragraph();
                                paragraph.add(phrase);
                                cell = new PdfPCell(paragraph);
								cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
								cell.setPaddingRight(40f);
								cell.setBorderWidth(0);
								inTable.addCell(cell);

								phrase = new Phrase(15, "");
								chunk = new Chunk("Adjustment", font);
   								phrase.add(chunk);
								paragraph = new Paragraph();
                                paragraph.add(phrase);
                                cell = new PdfPCell(paragraph);
								cell.setHorizontalAlignment(Element.ALIGN_LEFT);
								cell.setBorderWidth(0);
								inTable.addCell(cell);

								phrase = new Phrase(15, "");
								chunk = new Chunk(Adjustment, font);
   								phrase.add(chunk);
								paragraph = new Paragraph();
                                paragraph.add(phrase);
                                cell = new PdfPCell(paragraph);
								cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
								cell.setPaddingRight(40f);
								cell.setBorderWidth(0);
								inTable.addCell(cell);

								cell = new PdfPCell(inTable);
								table3.addCell(cell);

								//Create nested table to solve Rowspan problem
								PdfPTable nestedTable = new PdfPTable(1);

								//Reuse inTable for Current Charges in table3
								inTable = new PdfPTable(inTableColsWidth);

								//1
								phrase = new Phrase(15, "");
								chunk = new Chunk("Registration Charges", font);
   								phrase.add(chunk);
								paragraph = new Paragraph();
                                paragraph.add(phrase);
                                cell = new PdfPCell(paragraph);
								cell.setHorizontalAlignment(Element.ALIGN_LEFT);
								cell.setBorderWidth(0);
								inTable.addCell(cell);

								phrase = new Phrase(15, "");
								chunk = new Chunk(RegistrationCharges, font);
   								phrase.add(chunk);
								paragraph = new Paragraph();
                                paragraph.add(phrase);
                                cell = new PdfPCell(paragraph);
								cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
								cell.setBorderWidth(0);
								cell.setPaddingRight(40f);
								inTable.addCell(cell);

								//2
								phrase = new Phrase(15, "");
								chunk = new Chunk("Monthly Charges", font);
   								phrase.add(chunk);
								paragraph = new Paragraph();
                                paragraph.add(phrase);
                                cell = new PdfPCell(paragraph);
								cell.setHorizontalAlignment(Element.ALIGN_LEFT);
								cell.setBorderWidth(0);
								inTable.addCell(cell);

								phrase = new Phrase(15, "");
								chunk = new Chunk(MonthlyCharges, font);
   								phrase.add(chunk);
								paragraph = new Paragraph();
                                paragraph.add(phrase);
                                cell = new PdfPCell(paragraph);
								cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
								cell.setBorderWidth(0);
								cell.setPaddingRight(40f);
								inTable.addCell(cell);

								//3
								phrase = new Phrase(15, "");
								chunk = new Chunk("Value Added Services", font);
   								phrase.add(chunk);
								paragraph = new Paragraph();
                                paragraph.add(phrase);
                                cell = new PdfPCell(paragraph);
								cell.setHorizontalAlignment(Element.ALIGN_LEFT);
								cell.setBorderWidth(0);
								inTable.addCell(cell);

								phrase = new Phrase(15, "");
								chunk = new Chunk(ValueAddedServices, font);
   								phrase.add(chunk);
								paragraph = new Paragraph();
                                paragraph.add(phrase);
                                cell = new PdfPCell(paragraph);
								cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
								cell.setBorderWidth(0);
								cell.setPaddingRight(40f);
								inTable.addCell(cell);

								//4
								phrase = new Phrase(15, "");
								chunk = new Chunk("Usage Charges", font);
   								phrase.add(chunk);
								paragraph = new Paragraph();
                                paragraph.add(phrase);
                                cell = new PdfPCell(paragraph);
								cell.setHorizontalAlignment(Element.ALIGN_LEFT);
								cell.setBorderWidth(0);
								inTable.addCell(cell);

								phrase = new Phrase(15, "");
								chunk = new Chunk(UsageCharges, font);
   								phrase.add(chunk);
								paragraph = new Paragraph();
                                paragraph.add(phrase);
                                cell = new PdfPCell(paragraph);
								cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
								cell.setBorderWidth(0);
								cell.setPaddingRight(40f);
								inTable.addCell(cell);

								//5
								phrase = new Phrase(15, "");
								chunk = new Chunk("Advantage Savings", font);
   								phrase.add(chunk);
								paragraph = new Paragraph();
                                paragraph.add(phrase);
                                cell = new PdfPCell(paragraph);
								cell.setHorizontalAlignment(Element.ALIGN_LEFT);
								cell.setBorderWidth(0);
								inTable.addCell(cell);

								phrase = new Phrase(15, "");
								chunk = new Chunk(AdvantageSavings, font);
   								phrase.add(chunk);
								paragraph = new Paragraph();
                                paragraph.add(phrase);
                                cell = new PdfPCell(paragraph);
								cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
								cell.setBorderWidth(0);
								cell.setPaddingRight(40f);
								inTable.addCell(cell);

								//6
								phrase = new Phrase(15, "");
								chunk = new Chunk("Other Charges & Credit", font);
   								phrase.add(chunk);
								paragraph = new Paragraph();
                                paragraph.add(phrase);
                                cell = new PdfPCell(paragraph);
								cell.setHorizontalAlignment(Element.ALIGN_LEFT);
								cell.setBorderWidth(0);
								inTable.addCell(cell);

								phrase = new Phrase(15, "");
								chunk = new Chunk(OtherCharges, font);
   								phrase.add(chunk);
								paragraph = new Paragraph();
                                paragraph.add(phrase);
                                cell = new PdfPCell(paragraph);
								cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
								cell.setBorderWidth(0);
								cell.setPaddingRight(40f);
								inTable.addCell(cell);

								//7
								phrase = new Phrase(15, "");
								chunk = new Chunk("Discount & Rebates", font);
   								phrase.add(chunk);
								paragraph = new Paragraph();
                                paragraph.add(phrase);
                                cell = new PdfPCell(paragraph);
								cell.setHorizontalAlignment(Element.ALIGN_LEFT);
								cell.setBorderWidth(0);
								inTable.addCell(cell);

								phrase = new Phrase(15, "");
								chunk = new Chunk(Discount, font);
   								phrase.add(chunk);
								paragraph = new Paragraph();
                                paragraph.add(phrase);
                                cell = new PdfPCell(paragraph);
								cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
								cell.setBorderWidth(0);
								cell.setPaddingRight(40f);
								inTable.addCell(cell);

								cell = new PdfPCell(inTable);
								nestedTable.addCell(cell);

								//Reuse inTable for Current Charges before tax in table3
								inTable = new PdfPTable(inTableColsWidth);

								//1
								phrase = new Phrase(15, "");
								chunk = new Chunk("Current Charges Before Tax", font);
   								phrase.add(chunk);
								paragraph = new Paragraph();
                                paragraph.add(phrase);
                                cell = new PdfPCell(paragraph);
								cell.setHorizontalAlignment(Element.ALIGN_LEFT);
								cell.setBorderWidth(0);
								inTable.addCell(cell);

								phrase = new Phrase(15, "");
								chunk = new Chunk(CurrentChargesBeforeTax, font);
   								phrase.add(chunk);
								paragraph = new Paragraph();
                                paragraph.add(phrase);
                                cell = new PdfPCell(paragraph);
								cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
								cell.setBorderWidth(0);
								cell.setPaddingRight(40f);
								inTable.addCell(cell);

								//2
								phrase = new Phrase(15, "");
								chunk = new Chunk("Service Tax (5%)", font);
   								phrase.add(chunk);
								paragraph = new Paragraph();
                                paragraph.add(phrase);
                                cell = new PdfPCell(paragraph);
								cell.setHorizontalAlignment(Element.ALIGN_LEFT);
								cell.setBorderWidth(0);
								inTable.addCell(cell);

								phrase = new Phrase(15, "");
								chunk = new Chunk(ServiceTax, font);
   								phrase.add(chunk);
								paragraph = new Paragraph();
                                paragraph.add(phrase);
                                cell = new PdfPCell(paragraph);
								cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
								cell.setBorderWidth(0);
								cell.setPaddingRight(40f);
								inTable.addCell(cell);

								//add intable to nestedTable
								cell = new PdfPCell(inTable);
								nestedTable.addCell(cell);

								//add nestedTable to table3
								cell = new PdfPCell(nestedTable);
								table3.addCell(cell);

								//reused inTable for balance forward
								inTable = new PdfPTable(inTableColsWidth);

                                phrase = new Phrase(15, "");
								chunk = new Chunk("Balance Forward\n", fontBold);
								phrase.add(chunk);
								chunk = new Chunk("Baki Ke Hadapan", fontItalic);
								phrase.add(chunk);
								paragraph = new Paragraph();
                                paragraph.add(phrase);
                                cell = new PdfPCell(paragraph);
								cell.setHorizontalAlignment(Element.ALIGN_LEFT);
								cell.setBorderWidth(0);
								inTable.addCell(cell);

								phrase = new Phrase(15, "");
								chunk = new Chunk(BalanceForward, font);
								phrase.add(chunk);
								paragraph = new Paragraph();
                                paragraph.add(phrase);
                                cell = new PdfPCell(paragraph);
								cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
								cell.setBorderWidth(0);
								cell.setPaddingRight(40f);
								inTable.addCell(cell);

								cell = new PdfPCell(inTable);
								table3.addCell(cell);

								//reused inTable for Total Current Charges
								inTable = new PdfPTable(inTableColsWidth);

                                phrase = new Phrase(15, "");
								chunk = new Chunk("Total Current Charges\n", fontBold);
								phrase.add(chunk);
								chunk = new Chunk("Jumlah Caj Semasa", fontItalic);
								phrase.add(chunk);
								paragraph = new Paragraph();
                                paragraph.add(phrase);
                                cell = new PdfPCell(paragraph);
								cell.setHorizontalAlignment(Element.ALIGN_LEFT);
								cell.setBorderWidth(0);
								inTable.addCell(cell);

								phrase = new Phrase(15, "");
								chunk = new Chunk(TotalCurrentCharges, font);
								phrase.add(chunk);
								paragraph = new Paragraph();
                                paragraph.add(phrase);
                                cell = new PdfPCell(paragraph);
								cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
								cell.setBorderWidth(0);
								cell.setPaddingRight(40f);
								inTable.addCell(cell);

								cell = new PdfPCell(inTable);
								table3.addCell(cell);

								//reused inTable for Total Amount Due
								inTable = new PdfPTable(inTableColsWidth);

                                phrase = new Phrase(15, "");
								chunk = new Chunk("Total Amount Due\n", fontBold);
								phrase.add(chunk);
								chunk = new Chunk("Jumlah Perlu Dibayar", fontItalic);
								phrase.add(chunk);
								paragraph = new Paragraph();
                                paragraph.add(phrase);
                                cell = new PdfPCell(paragraph);
								cell.setHorizontalAlignment(Element.ALIGN_LEFT);
								cell.setBorderWidth(0);
								inTable.addCell(cell);

								phrase = new Phrase(15, "");
								chunk = new Chunk(TotalAmountDue, fontBold);
								phrase.add(chunk);
								paragraph = new Paragraph();
                                paragraph.add(phrase);
                                cell = new PdfPCell(paragraph);
								cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
								cell.setBorderWidth(0);
								cell.setPaddingRight(40f);
								inTable.addCell(cell);

								cell = new PdfPCell(inTable);
								cell.setColspan(2);
								table3.addCell(cell);


								//add table3 to main Table
								cell = new PdfPCell(table3);
								cell.setHorizontalAlignment(Element.ALIGN_CENTER);
								cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
								cell.setColspan(2);
								cell.setBorderWidth(0);
								table.addCell(cell);

								//add blank row at main table
								cell = new PdfPCell(new Paragraph("          "));
								cell.setHorizontalAlignment(Element.ALIGN_CENTER);
								cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
								cell.setColspan(2);
								cell.setBorderWidth(0);
								table.addCell(cell);

								cell = new PdfPCell(new Paragraph("          "));
								cell.setHorizontalAlignment(Element.ALIGN_CENTER);
								cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
								cell.setColspan(2);
								cell.setBorderWidth(0);
								table.addCell(cell);



								//create table4 for Important Message section
								PdfPTable table4 = new PdfPTable(1);

                                phrase = new Phrase(15, "");
								chunk = new Chunk("Important Message\n", fontBold);
								phrase.add(chunk);
								chunk = new Chunk("Maklumat Penting", fontItalic);
								phrase.add(chunk);
								paragraph = new Paragraph();
                                paragraph.add(phrase);
                                cell = new PdfPCell(paragraph);
								cell.setHorizontalAlignment(Element.ALIGN_CENTER);
								cell.setGrayFill(0.8f);
								table4.addCell(cell);

								phrase = new Phrase(15, "");
								chunk = new Chunk(MessageLine1, font);
   								phrase.add(chunk);
   								chunk = new Chunk(MessageLine2, font);
   								phrase.add(chunk);
   								chunk = new Chunk(MessageLine3, font);
   								phrase.add(chunk);
   								chunk = new Chunk(MessageLine4 + "\n\n", font);
   								phrase.add(chunk);
   								chunk = new Chunk(MessageLine5, fontItalic);
   								phrase.add(chunk);
   								chunk = new Chunk(MessageLine6, fontItalic);
   								phrase.add(chunk);
   								chunk = new Chunk(MessageLine7, fontItalic);
   								phrase.add(chunk);
   								chunk = new Chunk(MessageLine8, fontItalic);
   								phrase.add(chunk);
   								chunk = new Chunk(MessageLine9 + "\n\n", font);
   								phrase.add(chunk);
								paragraph = new Paragraph();
                                paragraph.add(phrase);
                                cell = new PdfPCell(paragraph);
								cell.setHorizontalAlignment(Element.ALIGN_LEFT);
								table4.addCell(cell);

								cell = new PdfPCell(table4);
								cell.setHorizontalAlignment(Element.ALIGN_CENTER);
								cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
								cell.setColspan(2);
								cell.setBorderWidth(0);
								table.addCell(cell);

								//add blank row at main table
								cell = new PdfPCell(new Paragraph("          "));
								cell.setHorizontalAlignment(Element.ALIGN_CENTER);
								cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
								cell.setColspan(2);
								cell.setBorderWidth(0);
								table.addCell(cell);

								cell = new PdfPCell(new Paragraph("          "));
								cell.setHorizontalAlignment(Element.ALIGN_CENTER);
								cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
								cell.setColspan(2);
								cell.setBorderWidth(0);
								table.addCell(cell);

								//add FIX content to main table
								phrase = new Phrase(15, "For mail in payment, please detach and send this portion together with your payment. Do not staple. Please complete details on reverse side. ", font);
								paragraph = new Paragraph();
                                paragraph.add(phrase);
                                phrase = new Phrase(0, "Untuk bayaran melalui POS, sila kirimkan keratan ini bersama bayaran anda. Sila lengkapkan bahagian belakang slip ini.", fontItalic);
                                paragraph.add(phrase);
								cell = new PdfPCell(paragraph);
								cell.setHorizontalAlignment(Element.ALIGN_LEFT);
								cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
								//cell.setColspan(2);
								cell.setBorderWidth(0);
								table.addCell(cell);

								//Title
								phrase = new Phrase(20, "");
								chunk = new Chunk("PAYMENT SLIP\n", bigFontBold);
								phrase.add(chunk);
								chunk = new Chunk("SLIP BAYARAN", bigFontItalic);
								phrase.add(chunk);
								paragraph = new Paragraph();
                                paragraph.add(phrase);
                                cell = new PdfPCell(paragraph);
								cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
								cell.setVerticalAlignment(Element.ALIGN_BOTTOM);
								//cell.setColspan(2);
								cell.setBorderWidth(0);
								table.addCell(cell);

								//add blank row at main table
								cell = new PdfPCell(new Paragraph("          "));
								cell.setHorizontalAlignment(Element.ALIGN_CENTER);
								cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
								cell.setColspan(2);
								cell.setBorderWidth(0);
								table.addCell(cell);

								//Repeat Account Detail at bottom page

								phrase = new Phrase(15, "");
								chunk = new Chunk("Account Name/", font);
								phrase.add(chunk);
								chunk = new Chunk("Nama Akaun       ", fontItalic);
								phrase.add(chunk);
								chunk = new Chunk(": " + AccName + "\n\n", font);
								phrase.add(chunk);


								chunk = new Chunk("Account No/", font);
								phrase.add(chunk);
								chunk = new Chunk("No.Akaun                 ", fontItalic);
								phrase.add(chunk);
								chunk = new Chunk(": " + AccNo + "\n\n", font);
								phrase.add(chunk);

								chunk = new Chunk("Phone Number/", font);
								phrase.add(chunk);
								chunk = new Chunk("No.Telefon          ", fontItalic);
								phrase.add(chunk);
								chunk = new Chunk(": " + PhoneNo + "\n\n", font);
								phrase.add(chunk);

								chunk = new Chunk("Reference Number/", font);
								phrase.add(chunk);
								chunk = new Chunk("No.Rujukan   ", fontItalic);
								phrase.add(chunk);
								chunk = new Chunk(": " + RefNo + "\n\n", font);
								phrase.add(chunk);

								paragraph = new Paragraph();
                                paragraph.add(phrase);

                                cell = new PdfPCell(paragraph);
								cell.setHorizontalAlignment(Element.ALIGN_LEFT);
								cell.setBorderWidth(0);
		                        table.addCell(cell);

		                        //Repeat Total Amount Due and current charges due date

 								table2 = new PdfPTable(2);

                                //table2.setWidthPercentage(100);

								//phrase = new Phrase(15, "\n\n");
								phrase = new Phrase(20, "");
								chunk = new Chunk("\nTotal Amount Due\n", fontBold);
								phrase.add(chunk);
								chunk = new Chunk("Jumlah Perlu Dibayar\n\n", fontItalic);
								phrase.add(chunk);
								paragraph = new Paragraph();
                                paragraph.add(phrase);
                                cell = new PdfPCell(paragraph);
								cell.setHorizontalAlignment(Element.ALIGN_CENTER);
								//cell.setBackgroundColor(Color.GREEN);
								cell.setGrayFill(0.8f);
								//cell.setMinimumHeight(10);
								//cell.setColspan(2);
								table2.addCell(cell);

								//phrase = new Phrase(15, "\n\n");
								phrase = new Phrase(20, "");
								chunk = new Chunk("\nCurrent Charges Due Date\n", fontBold);
								phrase.add(chunk);
								chunk = new Chunk("Tarikh Akhir Bayaran Caj Semasa\n\n", fontItalic);
								phrase.add(chunk);
								paragraph = new Paragraph();
                                paragraph.add(phrase);
								cell = new PdfPCell(paragraph);
								cell.setHorizontalAlignment(Element.ALIGN_CENTER);
								//cell.setBackgroundColor(Color.GREEN);
								cell.setGrayFill(0.8f);
								//cell.setMinimumHeight(10);
		                        table2.addCell(cell);

		                        cell = new PdfPCell(new Paragraph("\n" + TotalAmountDue + "\n\n", fontBold));
		                        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
		                        //cell.setMinimumHeight(10);
		   						table2.addCell(cell);

		   						cell = new PdfPCell(new Paragraph("\n" + CurrentChargesDueDate + "\n\n", fontBold));
		   						cell.setHorizontalAlignment(Element.ALIGN_CENTER);
		   						//cell.setMinimumHeight(10);
		   						table2.addCell(cell);

								cell = new PdfPCell(table2);
								cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
								cell.setVerticalAlignment(Element.ALIGN_BOTTOM);
								cell.setBorderWidth(0);
								table.addCell(cell);

		   						document.add(table);

			document.close();

        } catch (ServletException e) {
            // Just re-throw these
            throw e;
        } catch (Throwable t) {
            throw new ServletException(t.toString(), t);
        }
    }

}